package com.abvnet.project.water.sewage.controller;

import com.abvnet.framework.aspectj.lang.annotation.Log;
import com.abvnet.framework.aspectj.lang.enums.BusinessType;
import com.abvnet.framework.web.controller.BaseController;
import com.abvnet.framework.web.domain.AjaxResult;
import com.abvnet.framework.web.page.TableDataInfo;
import com.abvnet.project.water.project.domain.ProjectInfo;
import com.abvnet.project.water.project.domain.ProjectMicrobial;
import com.abvnet.project.water.project.service.IProjectInfoService;
import com.abvnet.project.water.project.service.IProjectMicrobialService;
import com.abvnet.project.water.sewage.domain.SewagePlan;
import com.abvnet.project.water.sewage.domain.SewagePlanMicrobial;
import com.abvnet.project.water.sewage.domain.SewagePlanProject;
import com.abvnet.project.water.sewage.service.ISewagePlanService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 解决方案信息操作处理
 * 
 * @author duj
 * @date 2019-07-30
 */
@Controller
@RequestMapping("/sewage/sewagePlan")
public class SewagePlanController extends BaseController {
    private String prefix = "sewage/sewagePlan";
	
	@Autowired
	private ISewagePlanService sewagePlanService;

    @Autowired
    private IProjectMicrobialService projectMicrobialService;

    @Autowired
    private IProjectInfoService projectInfoService;
	
	@RequiresPermissions("sewage:sewagePlan:list")
	@GetMapping()
	public String sewagePlan()
	{
	    return prefix + "/list";
	}
	
	/**
	 * 查询解决方案列表
	 */
	@RequiresPermissions("sewage:sewagePlan:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(SewagePlan sewagePlan) {
		startPage();
        List<SewagePlan> list = sewagePlanService.selectSewagePlanList(sewagePlan);
		return getDataTable(list);
	}
	
	/**
	 * 新增解决方案
	 */
	@GetMapping("/add")
	public String add() {
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存解决方案
	 */
	@RequiresPermissions("sewage:sewagePlan:add")
	@Log(title = "解决方案", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(SewagePlan sewagePlan) {
		return toAjax(sewagePlanService.insertSewagePlan(sewagePlan));
	}

	/**
	 * 修改解决方案
	 */
	@GetMapping("/edit/{planId}")
	public String edit(@PathVariable("planId") Long planId, ModelMap mmap) {
		SewagePlan sewagePlan = sewagePlanService.selectSewagePlanById(planId);
		mmap.put("sewagePlan", sewagePlan);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存解决方案
	 */
	@RequiresPermissions("sewage:sewagePlan:edit")
	@Log(title = "解决方案", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(SewagePlan sewagePlan) {
		return toAjax(sewagePlanService.updateSewagePlan(sewagePlan));
	}
	
	/**
	 * 删除解决方案
	 */
	@RequiresPermissions("sewage:sewagePlan:remove")
	@Log(title = "解决方案", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(sewagePlanService.deleteSewagePlanByIds(ids));
	}

    /**
     * 分配产品
     */
    @RequiresPermissions("sewage:sewagePlan:edit")
    @GetMapping("/assignMicro/{planId}")
    public String assignMicro(@PathVariable("planId") Long planId, ModelMap mmap) {
        mmap.put("planId", planId);
        return prefix + "/assignMicro";
    }

    @RequiresPermissions("sewage:sewagePlan:list")
    @PostMapping("/microList")
    @ResponseBody
    public TableDataInfo microList(Long planId) {
        startPage();
        List<ProjectMicrobial> list = projectMicrobialService.selectMicrobialListByPlanId(planId);
        return getDataTable(list);
    }

    /**
     * 取消指定
     */
    @Log(title = "解决方案", businessType = BusinessType.GRANT)
    @PostMapping("/microCancel")
    @ResponseBody
    public AjaxResult microCancel(SewagePlanMicrobial sewagePlanMicrobial) {
        return toAjax(sewagePlanService.cancelAssignMicro(sewagePlanMicrobial));
    }

    /**
     * 批量取消指定
     */
    @Log(title = "解决方案", businessType = BusinessType.GRANT)
    @PostMapping("/microCancelAll")
    @ResponseBody
    public AjaxResult microCancelAll(Long planId, String microbialIds) {
        return toAjax(sewagePlanService.cancelAssignMicroAll(planId, microbialIds));
    }

    /**
     * 选择菌剂
     */
    @GetMapping("/selectMicro/{planId}")
    public String selectMicro(@PathVariable("planId") Long planId, ModelMap mmap) {
        mmap.put("planId", planId);
        return prefix + "/selectMicro";
    }

    @RequiresPermissions("sewage:sewagePlan:list")
    @PostMapping("/unMicroList")
    @ResponseBody
    public TableDataInfo unMicroList(Long planId) {
        startPage();
        List<ProjectMicrobial> list = projectMicrobialService.selectUnMicrobialListByPlanId(planId);
        return getDataTable(list);
    }

    @Log(title = "解决方案", businessType = BusinessType.GRANT)
    @PostMapping("/microSelectAll")
    @ResponseBody
    public AjaxResult microSelectAll(Long planId, String microbialIds) {
        return toAjax(sewagePlanService.insertAssingMicros(planId, microbialIds));
    }

    /**
     * 分配项目
     */
    @RequiresPermissions("sewage:sewagePlan:edit")
    @GetMapping("/assignProject/{planId}")
    public String assignProject(@PathVariable("planId") Long planId, ModelMap mmap) {
        mmap.put("planId", planId);
        return prefix + "/assignProject";
    }

    @RequiresPermissions("sewage:sewagePlan:list")
    @PostMapping("/projectList")
    @ResponseBody
    public TableDataInfo projectList(Long planId) {
        startPage();
        List<ProjectInfo> list = projectInfoService.selectProjectListByPlanId(planId);
        return getDataTable(list);
    }

    /**
     * 取消指定
     */
    @Log(title = "解决方案", businessType = BusinessType.GRANT)
    @PostMapping("/projectCancel")
    @ResponseBody
    public AjaxResult projectCancel(SewagePlanProject sewagePlanProject) {
        return toAjax(sewagePlanService.cancelAssignProject(sewagePlanProject));
    }

    /**
     * 批量取消指定
     */
    @Log(title = "解决方案", businessType = BusinessType.GRANT)
    @PostMapping("/projectCancelAll")
    @ResponseBody
    public AjaxResult projectCancelAll(Long planId, String projectIds) {
        return toAjax(sewagePlanService.cancelAssignProjectAll(planId, projectIds));
    }

    /**
     * 选择项目
     */
    @GetMapping("/selectProject/{planId}")
    public String selectProject(@PathVariable("planId") Long planId, ModelMap mmap) {
        mmap.put("planId", planId);
        return prefix + "/selectProject";
    }

    @RequiresPermissions("sewage:sewagePlan:list")
    @PostMapping("/unProjectList")
    @ResponseBody
    public TableDataInfo unProjectList(Long planId) {
        startPage();
        List<ProjectInfo> list = projectInfoService.selectUnProjectListByPlanId(planId);
        return getDataTable(list);
    }

    @Log(title = "解决方案", businessType = BusinessType.GRANT)
    @PostMapping("/projectSelectAll")
    @ResponseBody
    public AjaxResult projectSelectAll(Long planId, String projectIds) {
        return toAjax(sewagePlanService.insertAssingProjects(planId, projectIds));
    }
}
